home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ccdl150l.zip / SORT / QSORT.C < prev    next >
C/C++ Source or Header  |  1996-07-03  |  522b  |  26 lines

  1. #include <stdlib.h>
  2. #include <memory.h>
  3.  
  4. void qsort(const void *base, size_t num, size_t width,
  5.                         int (*compare)(const void *elem1, const void *elem2))
  6. {
  7.     int i,j;
  8.     void *buf = malloc(width);
  9.     char *wp1 = base;
  10.     if (!buf)
  11.         return;
  12.     for (i=0; i < num-1;i++) {
  13.         char *wp2 = wp1 + width;
  14.         for (j=i+1; j < num; j++) {
  15.             if ((*compare)(wp1,wp2) >0) {
  16.                 memcpy(buf,wp1,width);
  17.                 memcpy(wp1,wp2,width);
  18.                 memcpy(wp2,buf,width);
  19.             }
  20.             wp2 += width;
  21.         }
  22.         wp1 += width;
  23.     }
  24.     free(buf);
  25.             
  26. }